package thinkinjava.c21_concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Auther DRevelation
 * @Date 2020-05-11 下午 16:44
 * @Desc 使用sleep后，每个线程都有100ms以内的时间去完成任务，所以打印的都是递减的数据
 * java编程思想得出的 完美的运行 不太确切，只是不像Thread.yield()那么混乱
 */
public class SleepingTask extends LiftOff {

    @Override
    public void run() {
        try {
            while (countDown-- > 0) {
                System.out.print(status());
                // Thread.sleep(100);
                TimeUnit.MICROSECONDS.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            exec.submit(new SleepingTask());
        }
        exec.shutdown();
    }
}
